package xyz.zhzby.cyzx.oj.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

/**
 * @author zby
 * @date 2021/12/08
 */
@Configuration
public class JacksonLongToStringConfig implements WebMvcConfigurer {

    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.stream()
                .filter(e -> e instanceof MappingJackson2HttpMessageConverter)
                .forEach(e -> {
                    ObjectMapper objectMapper = ((MappingJackson2HttpMessageConverter) e).getObjectMapper();
                    SimpleModule simpleModule = new SimpleModule();
                    ToStringSerializer toStringSerializer = new ToStringSerializer();
                    simpleModule.addSerializer(Long.class, toStringSerializer);
                    simpleModule.addSerializer(Long.TYPE, toStringSerializer);
                    objectMapper.registerModule(simpleModule);
                });
    }
}
